//
//  VerifyPhoneController.m
//  lhpc_manager
//
//  Created by 赵永杰 on 2017/9/6.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "VerifyPhoneController.h"
#import <HLBaseControl/HLGetCodeButton.h>
#import "ResetPasswordViewController.h"
#import "LHNetworking.h"
#import <SMS_SDK/SMSSDK.h>

@interface VerifyPhoneController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;

@property (weak, nonatomic) IBOutlet UITextField *codeTF;

@property (weak, nonatomic) IBOutlet HLGetCodeButton *getCodeBtn;

@property (weak, nonatomic) IBOutlet UIButton *nextBtn;

@end

@implementation VerifyPhoneController

#pragma mark - LifeCycle

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.phoneTF becomeFirstResponder];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"验证手机";
    
    [self setupChildViews];
}


#pragma mark - Public

#pragma mark - Private

- (void)setupChildViews {
    self.getCodeBtn.layer.cornerRadius = 2;
    self.nextBtn.layer.cornerRadius = 4;
    _phoneTF.text = [LHGetUserPhone() stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
}

- (IBAction)getCodeAction:(HLGetCodeButton *)sender {

    [self requestMobileCodeWithBtn:sender];
    
}

- (void)requestMobileCodeWithBtn:(HLGetCodeButton *)sender {
    
//    LHPC_WEAK_SELF;
    [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleLoading status:@"正在发送验证码"];
    [SMSSDK getVerificationCodeByMethod:0 phoneNumber:LHGetUserPhone() zone:@"86" result:^(NSError *error) {
//        LHPC_STRONG_SELF;
        [HLProgressHUD dismissHUD];
        if (!error) {
            NSLog(@"获取验证码成功");
            [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleSuccess status:@"发送成功"];
            [sender click];
            
        } else {
            [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleError status:@"验证码发送上限"];
        };
    }];
    
}

- (IBAction)nextBtnAction:(UIButton *)sender {
    
    if (_codeTF.text.length != 4) {
        [HLProgressHUD showHudTipStr:@"请输入正确的验证码"];
        return;
    }
    
    
    [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleLoading status:@"正在验证"];
    LHPC_WEAK_SELF;
    [[LHNetworking shared] POST:LHSetRequestAPI(LHPasswordVerficaPhone) parameters:@{@"mobile":LHGetUserPhone(),@"code":_codeTF.text} success:^(NSURLSessionDataTask *task, id responseObject) {
        LHPC_STRONG_SELF;
        
        if (responseObject) {
            
            [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleSuccess status:@"验证成功"];
            
            ResetPasswordViewController * view = [[ResetPasswordViewController alloc]init];
            
            [strongSelf.navigationController pushViewController:view animated:YES];
            
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [HLProgressHUD showHudTipStr:LHNetWorkErrorMessage];
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.phoneTF) {
        [self.codeTF becomeFirstResponder];
    }else{
        [textField resignFirstResponder];
    }
    return YES;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}


#pragma mark - UITableViewDelegate

#pragma mark - Getter

#pragma mark - Setter

@end
